package com.diors.game.engine.arpg.scene;

import java.util.ArrayList;
import java.util.List;

/**
 * 场景分屏工具类
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013-10-23
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public final class ScreenUtils 
{
	
	private final static ScreenUtils instance = new ScreenUtils();
	
	private ScreenUtils (){}
	
	public static ScreenUtils getInstance ()
	{
		return instance;
	}
	
	/* 默认分屏的X与Y的格子数 */
	private final static int DEFULT_SCREEN_X = 20;
	private final static int DEFULT_SCREEN_Y = 15;
	
	/**
	 * 获取场景全部分屏的ID列表
	 * @param maxX	场景最大X坐标
	 * @param maxY	场景最大Y坐标
	 * @return
	 * @create	2013-10-23	darren.ouyang
	 */
	public List<Integer> getScreenIDs (int maxX, int maxY)
	{
		maxX += 1;
		maxY += 1;
		int screenX = getScreenX(maxX);
		int screenY = getScreenY(maxY);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 1; i <= screenX; i++)
		{
			for (int ii = 1; ii <= screenY; ii++)
				list.add(screenToID(i, ii));
		}
		
		return list;
	}
	
	/**
	 * 通过x,y坐标获取九宫格对应的分屏ID列表
	 * @param maxX	场景最大X坐标
	 * @param maxY	场景最大Y坐标
	 * @param x		当前x坐标
	 * @param y		当前y坐标
	 * @return
	 * @create	2013-10-23	darren.ouyang
	 */
	public List<Integer> getSudokuScreenIDs (int maxX, int maxY, int x, int y)
	{
		int screenX = getScreenX(x+1);
		int screenY = getScreenY(y+1);
		int screenMaxX = getScreenX(maxX+1);
		int screenMaxY = getScreenY(maxY+1);
		
		List<Integer> list = new ArrayList<Integer>();
		for (int ix = -1; ix<2; ix++)
		{
			int idx = ix + screenX;
			if (idx <= 0 || idx > screenMaxX)
				continue;
			
			for (int iy = -1; iy<2; iy++)
			{
				int idy = iy + screenY;
				if (idy <= 0 || idy > screenMaxY)
					continue;
				
				int id = screenToID(idx, idy);
				list.add(id);
			}
		}

		return list;
	}
	
	public int getScreenID (int x, int y)
	{
		int screenX = getScreenX(x+1);
		int screenY = getScreenY(y+1);
		return screenToID(screenX, screenY);
	}
	
	public int getScreenMinX (int screenID, int maxX, int maxY)
	{
		int screenX = idToScreenX(screenID);
		int val = DEFULT_SCREEN_X * (screenX - 1);
		return val;
	}
	
	public int getScreenMinY (int screenID, int maxX, int maxY)
	{
		int screenY = idToScreenY(screenID);
		int val = DEFULT_SCREEN_Y * (screenY - 1);
		return val;
	}
	
	public int getScreenMaxX (int screenID, int maxX, int maxY)
	{
		int screenX = idToScreenX(screenID);
		int val = DEFULT_SCREEN_X * screenX;
		if (val > maxX)
			return maxX;
		
		return val;
	}
	
	public int getScreenMaxY (int screenID, int maxX, int maxY)
	{
		int screenY = idToScreenY(screenID);
		int val = DEFULT_SCREEN_Y * screenY;
		if (val > maxY)
			return maxY;
		
		return val;
	}
	
	
	private int getScreenX (int x)
	{
		return x / DEFULT_SCREEN_X + (x%DEFULT_SCREEN_X == 0 ? 0 : 1);
	}
	
	private int getScreenY (int y)
	{
		return y / DEFULT_SCREEN_Y + (y%DEFULT_SCREEN_Y == 0 ? 0 : 1);
	}
	
	private int screenToID (int screenX, int screenY)
	{
		return (screenX << 10) + screenY; 
	}
	
	private int idToScreenX (int screenID)
	{
		return screenID >> 10; 
	}
	
	private int idToScreenY (int screenID)
	{
		return screenID - (idToScreenX(screenID) << 10); 
	}
	
	
	public static void main(String[] args) 
	{
		//int[] aa = SceneUtils.getInstance().getScreenIDs(8, 21);
		int maxX = 0;
		int maxY = 21;
		List<Integer> list = ScreenUtils.getInstance().getScreenIDs(maxX, maxY);
		
		for (int value : list)
		{
			int a =  ScreenUtils.getInstance().getScreenMinX(value, maxX, maxY);
			int b =  ScreenUtils.getInstance().getScreenMinY(value, maxX, maxY);
			int c =  ScreenUtils.getInstance().getScreenMaxX(value, maxX, maxY);
			int d =  ScreenUtils.getInstance().getScreenMaxY(value, maxX, maxY);
			System.out.println(value +" [" + a + "," + b + "," + c + "," + d + "] ");
			
		}
		
		
	}
}
